﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.SellStockSearchWrite
{
    public class SellStockUpdateHandler : IRequestHandler<SellStockUpdateCommand, ResultsDTO>
    {
        private readonly IBateRepository<SellStock> _sellstockRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sellstockRepository"></param>
        public SellStockUpdateHandler(IBateRepository<SellStock> sellstockRepository)
        {
            _sellstockRepository = sellstockRepository;
        }
        /// <summary>
        /// 销售出库单修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(SellStockUpdateCommand request, CancellationToken cancellationToken)
        {
            var list = new ResultsDTO() { Code = 0 };

            //连接数据库
            DapperHelper dapper = new DapperHelper();
            var conn = dapper.MangoMesWrite;

            //实体
            var sellStock = new SellStock();
            var str = "update sellstock set SellStockNo='" + request.SellStockNo + "',SellStockName='" +request.SellStockName+ "',SellStockIndent='" + request.SellStockIndent + "',ClientId='" + request.ClientId + "',SellStockTime='" + request.SellStockTime + "'," +
                "SellStockStart='" + request.SellStockStart + "',SellStockRemark='" + request.SellStockRemark + "',ProductsSaleId='" + request.ProductsSaleId + "' where SellStockId='" + request.SellStockId + "'";
            var result = _sellstockRepository.Add(str, sellStock, conn);
            if (result != null)
            {
                list.Code = 1;
                list.Message = "修改成功";
            }
            return list;
        }
    }
}
